<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 *      [PinTuXiu] (C)2001-2099 ONightjar.com Pintuxiu.com.
 *      This is NOT a freeware, use is subject to license terms
*/
class ugroup_db extends Model{
	public $pk = 'user_group_id';
	public $table = 'ptx_user_group';
	var $linker = array(
			'pgroup' => 	array(
					'type' => 'hasone',
					'map' => 'pgroup',
					'mapkey' => 'group_id',
					'ftable' => 'ptx_group',
					'fkey' => 'group_id',
					'enabled' => true
			),
			'user' => 	array(
					'type' => 'hasone',
					'map' => 'user',
					'mapkey' => 'user_id',
					'ftable' => 'ptx_user',
					'fkey' => 'user_id',
					'enabled' => true
			)
	);

	var $other_join = array(
			'owner' => 	array(
					'main_table' => 'ptx_user',
					'main_alias' => 'owner',
					'main_fkey' => 'user_id',
					'sec_table_alias' => 'pgroup',
					'sec_mapkey' => 'user_id',
					'enabled' => true
			),
			'category' => 	array(
					'main_table' => 'ptx_category',
					'main_alias' => 'category',
					'main_fkey' => 'category_id',
					'sec_table_alias' => 'pgroup',
					'sec_mapkey' => 'category_id',
					'enabled' => true
			)
	);

	var $select_fields = " pgroup.*,owner.user_id,owner.nickname,owner.user_title,category.category_id,category.category_name_cn ";

	private function init_conditions($conditions){
		$conditions_group = NULL;
		if(isset($conditions['user_id'])){
			$conditions_group .= 'AND ptx_user_group.user_id='.$conditions['user_id'].' ';
		}

		if(isset($conditions['category_id'])){
			$conditions_group .= 'AND pgroup.category_id='.$conditions['category_id'].' ';
		}

		if(isset($conditions['group_id'])){
			$conditions_group .= 'AND ptx_user_group.group_id='.$conditions['group_id'].' ';
		}

		if(isset($conditions['is_admin'])){
			$conditions_group .= 'AND ptx_user_group.is_admin='.$conditions['is_admin'].' ';
		}
		if($conditions_group){
			if(strpos($conditions_group, 'AND') === 0){
				$conditions_group = substr($conditions_group, 3);
			}
		}
		return $conditions_group;
	}

	public function search($conditions=NULL,$page,$pagesize,$fields = null){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_user_group.create_time DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->pager($page, $pagesize)->findAllJoin($conditions,$sort,$fields);
	}

	public function search_no_page($conditions=NULL,$fields = null,$sort=null,$limit){
		if(is_array($conditions)) $conditions = $this->init_conditions($conditions);
		$sort = isset($sort)?$sort:" ptx_user_group.create_time DESC ";
		$fields = isset($fields)?$fields:$this->select_fields;
		return $this->findAllJoin($conditions,$sort,$fields,$limit);
	}

	public function find_one($conditions=NULL,$fields = null){
		if(!$fields){
			$fields = $this->select_fields;
		}
		return $this->findJoin($conditions,null,$fields);
	}

	public function add_one($data){
		if($this->check_value($data)){
			$data['create_time'] = TIMESTAMP;
			$result = $this->create($data);
			if($result) load_db('group')->update_total_member($data['group_id']);
			return $result;
		}
		return false;
	}

	public function remove_one($data){
		$result = $this->delete($data);
		if($result) load_db('group')->update_total_member($data['group_id']);
		return $result;
	}

	private function check_value($data){
		if(!is_numeric($data['user_id'])){
			return false;
		}
		if(!is_numeric($data['group_id'])){
			return false;
		}
		if($this->find_one($data)){
			return false;
		}

		return true;
	}

}
